//-----------------------------------------------------------------------
// <copyright file="EqualMatcherTest.cs" company="NMock2">
//
//   http://www.sourceforge.net/projects/NMock2
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
// </copyright>
// This is the easiest way to ignore StyleCop rules on this file, even if we shouldn't use this tag:
// <auto-generated />
//-----------------------------------------------------------------------
namespace NMock2.Test.Matchers
{
    using System.Collections;
    using NUnit.Framework;
    using NMock2.Matchers;

    [TestFixture]
    public class EqualMatcherTest
    {
        const string EXPECTED = "expected";
        
        [Test]
        public void ComparesArgumentForEqualityToExpectedObject()
        {
            Matcher matcher = new EqualMatcher(EXPECTED);
            
            Assert.IsTrue( matcher.Matches(EXPECTED), "same object" );
#if !SILVERLIGHT
            Assert.IsTrue( matcher.Matches(EXPECTED.Clone()), "equal object" );
#endif
            Assert.IsFalse( matcher.Matches("not expected"), "unequal object" );
        }
        
        [Test]
        public void IsNullSafe()
        {
            Assert.IsTrue( new EqualMatcher(null).Matches(null), "null matches null" );
            Assert.IsFalse( new EqualMatcher("not null").Matches(null), "not null does not match null" );
            Assert.IsFalse( new EqualMatcher(null).Matches("not null"), "null does not match not null" );
        }
        
        [Test]
        public void ComparesArraysForEqualityByContents()
        {
            int[] expected = {1,2};
            int[] equal = {1,2};
            int[] inequal = {2,3};
            int[] longer = {1,2,3};
            int[] shorter = {1};
            int[] empty = {};
            int[,] otherRank = {{1,2},{3,4}};
            Matcher matcher = new EqualMatcher(expected);
            
            Assert.IsTrue(matcher.Matches(expected), "same array");
            Assert.IsTrue(matcher.Matches(equal), "same contents");
            Assert.IsFalse(matcher.Matches(inequal), "different contents");
            Assert.IsFalse(matcher.Matches(longer), "longer");
            Assert.IsFalse(matcher.Matches(shorter), "shorter");
            Assert.IsFalse(matcher.Matches(empty), "empty");
            Assert.IsFalse(matcher.Matches(otherRank), "other rank");
        }
            
        [Test]
        public void ComparesMultidimensionalArraysForEquality()
        {
            int[,] expected = {{1,2},{3,4}};
            int[,] equal = {{1,2},{3,4}};
            int[,] inequal = {{3,4},{5,6}};
            int[,] empty = new int[0,0];
            int[] otherRank = {1,2};
            Matcher matcher = new EqualMatcher(expected);
            
            Assert.IsTrue(matcher.Matches(expected), "same array");
            Assert.IsTrue(matcher.Matches(equal), "same contents");
            Assert.IsFalse(matcher.Matches(inequal), "different contents");
            Assert.IsFalse(matcher.Matches(empty), "empty");
            Assert.IsFalse(matcher.Matches(otherRank), "other rank");
        }
            
        [Test]
        public void RecursivelyComparesArrayContentsOfNestedArrays()
        {
            int[][] expected = new int[][] {new int[]{1,2},new int[]{3,4}};
            int[][] equal = new int[][] {new int[]{1,2},new int[]{3,4}};
            int[][] inequal = new int[][] {new int[]{2,3},new int[]{4,5}};
            Matcher matcher = new EqualMatcher(expected);
            
            Assert.IsTrue(matcher.Matches(expected), "same array");
            Assert.IsTrue(matcher.Matches(equal), "same contents");
            Assert.IsFalse(matcher.Matches(inequal), "different contents");
        }

#if !SILVERLIGHT
        [Test]
        public void RecursivelyComparesContentsOfNestedLists()
        {
            ArrayList expected = new ArrayList(new ArrayList[]{
            new ArrayList(new int[]{1,2}),
            new ArrayList(new int[]{3,4})});
            ArrayList equal = new ArrayList(new ArrayList[]{
            new ArrayList(new int[]{1,2}),
            new ArrayList(new int[]{3,4})});
            ArrayList inequal = new ArrayList(new ArrayList[]{
            new ArrayList(new int[]{2,3}),
            new ArrayList(new int[]{4,5})});
            Matcher matcher = new EqualMatcher(expected);
            
            Assert.IsTrue(matcher.Matches(expected), "same array");
            Assert.IsTrue(matcher.Matches(equal), "same contents");
            Assert.IsFalse(matcher.Matches(inequal), "different contents");
        }
#endif
            
        [Test]
        public void CanCompareAutoboxedValues()
        {
            Matcher matcher = new EqualMatcher(1);
            Assert.IsTrue(matcher.Matches(1), "equal value");
            Assert.IsFalse(matcher.Matches(2), "other value");
        }

        [Test]
        public void HasAReadableDescription()
        {
            NamedObject value = new NamedObject("value");
            AssertDescription.IsEqual(new EqualMatcher(value), "equal to <"+value+">");
        }
    }
}
